﻿using System.Reflection;

namespace C_中const和readonly关键字以及readonly修饰的变量怎样修改
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "C#中const和readonly的区别";

            //Console.WriteLine(TestConst.constNum);
            //Console.WriteLine(TestConst.staticReadonlyNum);

            TestConst testconst = new TestConst();
            //testconst.readonlyNum = 2; //
            //testconst.listNums.Add(2);

            //testconst.listNums.ForEach(x => Console.WriteLine(x));  

            Student student = new Student();
            //Console.WriteLine(student.GetID());
            //反射方法修改值
            //FieldInfo fieldinfo = typeof(Student).GetField("ID", BindingFlags.Instance | BindingFlags.NonPublic);
            //fieldinfo.SetValue(student, 2);
            //Console.WriteLine(student.GetID()); ;

            Console.WriteLine(student.Grade);
            //student.Grade = 3;//无法修改
            //typeof(Student).GetProperty("Grade", BindingFlags.Instance | BindingFlags.Public)
            //    .SetValue(student,3); //异常
            //貌似net6.0不太行
            FieldInfo fieldinfo = typeof(Student).GetField("<Grade>k__BackingFiled", BindingFlags.Instance | BindingFlags.NonPublic);
            fieldinfo.SetValue(student, 3);
            Console.WriteLine(student.Grade);

            Console.ReadKey();
        }
    }
}
